package com.example.sonone.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.sonone.annotation.Router;
import com.example.sonone.dao.TestDao;
import com.example.sonone.pojo.BaseDomin;
import com.example.sonone.pojo.Order;
import com.example.sonone.pojo.Stu;
import com.example.sonone.service.TestService;
import com.github.crab2died.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("test")
public class ExcelController {


    @Autowired
    private TestService testService;

    @Autowired
    private TestDao testDao;

    @GetMapping("a")
    public List a() {

        Map<String, Object> params = new HashMap<>();
        params.put("offset", 0);
        params.put("size", 10);
        List<Map<String, Object>> list = testDao.selectTest(params);
        return list;

    }


    /**
     * 你他妈为何这么菜
     *
     * @param servletResponse
     */
    @PostMapping("excel")
    public void excel(HttpServletResponse servletResponse) {

        testService.exportExcel(servletResponse);
    }


    @GetMapping("excel2")
    public void excel2(HttpServletResponse servletResponse, String month) {

        testService.exportExcel2(servletResponse, month);
    }

    /**
     * excel4j 写法
     *
     * @param servletResponse
     * @throws Exception
     */
    @GetMapping("excel4j")
    public void excel4j(HttpServletResponse servletResponse) throws Exception {
        List<List<String>> list2 = new ArrayList<>();
        List<String> header = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<String> _list = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                _list.add(i + " -- " + j);
            }
            list2.add(_list);
            header.add(i + "---");
        }
//        ExcelUtils.getInstance().exportObjects2Excel(list2, header, "D:/D.xlsx"); // 直接写到本地磁盘
//        OutputStream out=new FileOutputStream("D:/D.xlsx");
        try {
            servletResponse.reset();
            //设置请求头
            servletResponse.setHeader("Access-Control-Allow-Origin", "*");
            servletResponse.setContentType("application/vnd.ms-excel;charset=utf-8");
            servletResponse.setHeader("Content-Disposition", "attachment;filename=" + new String(("test" + ".xls").getBytes(), "iso-8859-1"));
            ServletOutputStream out = servletResponse.getOutputStream();
            ExcelUtils.getInstance().exportObjects2Excel(list2, header, out);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 测试 haproxy
     */
    @PostMapping("haproxy")
    public void testHaproxy(@RequestBody Map<String, String> map) {

        testService.insert(map);

    }

    @PostMapping("testBaseDomin")
    @Router(routingFiled = "orderId")
    public void testBaseDomin(Order order) {
        BaseDomin b = new BaseDomin();
        b.setAa("zhangjianqiu");
        System.out.println(b.getTableSuffix());
        System.out.println(order.getTableSuffix());
        System.out.println(order);

    }

    /**
     * 测试 haproxy
     */
    @PostMapping("testnginx")
    public void testnginx() {
        Stu stu = new Stu(1, "qiuqiu");
        List<Stu> list = new ArrayList();
        list.add(stu);
        testDao.insertStu(list);
    }

    /**
     * 测试 mybatis
     *
     * @param args
     */
    @PostMapping("testmybatis")
    public List<Map<String, Object>> testmybatis(@RequestBody Map<String, Object> params) {
//        Map<String,Object>params=new HashMap<>();
//        params.put("offset",0);
//        params.put("size",10);
        return testDao.selectTest(params);
    }


    public static void main(String[] args) {
        Calendar beforeTime = Calendar.getInstance();
        Date nowD = beforeTime.getTime();

        beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
        Date beforeD = beforeTime.getTime();

        String before5 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(beforeD);  // 前五分钟时间
        String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(nowD);

        System.out.println(before5);
        System.out.println(now);

        System.out.println("=======================");

        Map<String, Object> map = new HashMap();
        map.put("hh", "qw121");
        map.put("cons", new Date());
        map.toString();
        System.out.println("map is : " + map);

        String str = JSON.toJSONString(map);
        System.out.println(str);

        //字符串转map
        JSONObject jsonObject = JSONObject.parseObject(str);
        Map<String, Object> mao = (Map<String, Object>) jsonObject;//    //json对象转Map
        System.out.println(mao.toString());

        String abc = "{\"hh\":\"qw121\",\"cons\":123}";

        JSONObject jsonObject2 = JSONObject.parseObject(abc);
        Map<String, Object> mao2 = (Map<String, Object>) jsonObject2;
        System.out.println("mao2 " + mao2);

        System.out.println("=======================");


        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("dd", "djsk1");
        jsonObject1.put("dd2", new Date());
        String s = jsonObject1.toJSONString();
        System.out.println(s);
        System.out.println("=======================");
        System.out.println("=======================");
        System.out.println("=======================");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add("qwqw12131");
        jsonArray.add(12121);
        jsonArray.add(new Date());
        System.out.println(jsonArray);
        System.out.println(jsonArray.toString());
        String ss1 = "[\"qwqw12131\",12121,1569306640782]";
        JSONArray json = JSONArray.parseArray(ss1);
        System.out.println("!!!!!!!!!!!: " + json);

        System.out.println("sssssssssssss");
        Object[] param = null;
        param = new Object[]{1, 2, "wdwew"};
        System.out.println(param.toString());
        int[] arr = {12, 3, 4, 8, 5, 6, 6, 7, 8, 8, 9, 8};
        System.out.println(arr.toString());

        String s1 = "Hello";
        String s2 = new String("Hello");
        String s3 = s2.intern();
        System.out.println(s2 == s3);

    }


}
